//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace WorkflowApplicationModels
{
    using Microsoft.Research.DataLayer;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Linq;
    using Microsoft.Research.eResearch.Common.Linq;
    using System.Collections.Generic;

    /// <summary>
    /// The category model.
    /// </summary>
    public class CategoryModel : BaseModel
    {
        /// <summary>
        /// The child models.
        /// </summary>
        private ObservableCollection<BaseModel> children = new ObservableCollection<BaseModel>();

        /// <summary>
        /// Gets the children.
        /// </summary>
        /// <value>The children.</value>
        public ObservableCollection<BaseModel> Children
        {
            get
            {
                return this.children;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryModel"/> class.
        /// </summary>
        /// <param name="category">The category.</param>
        public CategoryModel(Namespace category, User currentUser)
        {
            this.Name = category.Name;
            this.Label = (string.IsNullOrEmpty(category.Label)) ? this.Name : category.Label;
            this.Id = (category as IObject).ID;
            this.ItemIcon = BaseModel.CreateImage(@"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/WorkflowApplication/categoriesHeaderPic.png");

            if (currentUser != null)
            {
                List<WorkflowModel> tempList = new List<WorkflowModel>();
                foreach (Activity activity in category.Activitys)
                {
                    if (!activity.IsDeleted 
                        && (activity.Security.Rights.AuthorWorkflow.Check(currentUser)
                            || activity.Security.Rights.RunWorkflow.Check(currentUser)))
                    {
                        if (!string.IsNullOrEmpty(activity.VersionLabel) || activity.IsLatestVersion())
                        {
                            tempList.Add(new WorkflowModel(activity));
                        }
                    }
                }

                tempList.Sort(WorkflowModel.WorkflowComparer);
                tempList.ForEach(a => this.children.Add(a));
                category.Children.ForEach(ns => this.Children.Add(new CategoryModel(ns, currentUser)));
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryModel"/> class.
        /// </summary>
        /// <param name="category">The category.</param>
        private CategoryModel(string categoryLabel, IEnumerable<Activity> activities)
        {
            this.Name = categoryLabel;
            this.Label = categoryLabel;
            this.ItemIcon = BaseModel.CreateImage(@"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/WorkflowApplication/categoriesHeaderPic.png");

            foreach (Activity activity in activities)
            {
                if (!activity.IsDeleted && (!string.IsNullOrEmpty(activity.VersionLabel) || activity.IsLatestVersion()))
                {
                    this.Children.Add(new WorkflowModel(activity));
                }
            }
        }

        /// <summary>
        /// Generates the virtual category model.
        /// </summary>
        /// <param name="categoryName">Name of the category.</param>
        /// <param name="activities">The activities.</param>
        /// <returns></returns>
        public static CategoryModel GenerateVirtualCategoryModel(string categoryLabel, IEnumerable<Activity> activities)
        {
            return new CategoryModel(categoryLabel, activities);
        }   
    }
}
